/*
@author : SuoNi
@date : 2021/12/9 00:16
@Mail : suoni1919@gmail.com
@Description:切片扩容原理
*/
package main

import "fmt"

func main() {
	var a []int
	for i := 0; i <= 10; i++ {
		a = appendInt(a, i)
		fmt.Println(a, len(a), cap(a))
	}
}

func appendInt(x []int, y int) []int {
	var z []int
	zlen := len(x) + 1
	if zlen < cap(x) {
		z = x[:zlen]
	} else {
		zcap := zlen
		if zcap < 2*len(x) {
			zcap = 2 * len(x)
		}
		z = make([]int, zlen, zcap)
		copy(z, x)
	}
	z[len(x)] = y
	return z
}
